package com.cskaoyan.service;


import com.cskaoyan.bean.storage.Storage;
import com.cskaoyan.mapper.StorageMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.UUID;

@Service
public class AdminStorageServiceImp implements AdminStorageService {
    @Autowired
    StorageMapper storageMapper;
//
//    @Value("${url}")
//    private String contextPath;

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public Storage insertObject(MultipartFile file) {
//        String contentType = file.getContentType();
//        long size = file.getSize();
//        String name = file.getName();
//        String originalFilename = file.getOriginalFilename();
//        String tempFilename = UUID.randomUUID().toString();
//        String fileName = tempFilename + originalFilename;
//        Storage storage = new Storage();
//        storage.setKey(fileName);
//        storage.setName(originalFilename);
//        storage.setSize(size);
//        storage.setType(contentType);
//        storage.setUrl(contextPath + fileName);
//        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        Date date = null;
//        try {
//            date = objectMapper.readValue(dateTime, Date.class);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
//        storage.setAddTime(date);
//        storage.setUpdateTime(date);
//        File destFile = new File("D:\\mall", fileName);
//        try {
//            file.transferTo(destFile);
//        } catch (IOException e) {
//            return null;
//        }
//        int insert = storageMapper.insertSelective(storage);
//        if (insert == 0) {
//            return null;
//        }
//        return storage;
        return null;
    }
}
